/*window.ROOT = "";*/
(function() {
	// ///列表数据
	var serverData = {};
	serverData.key = "";
	var receivedPaymentsTotalInfoTable;
	jQuery(document).ready(function (){
	receivedPaymentsTotalInfoTable = jQuery('#receivedPaymentsTotalInfoTable')
			.DataTable(
					{
						"serverSide" : true,
						"ordering" : false,
						"ajax" : {
							"dataType" : "json",
							"type" : "get",
							"cache" : false,							
							"url" : window.rootPath
									+ '/ui/ReceivedPaymentTotalInfo/getPage',
							"data" : function(d) {
								return $.extend({}, d, serverData);
							}
						},
						columns : [
								{
									data : null,
									defaultContent : ""
								},
								{
									data : 'sReceivedPaymentsNetworkNumer'
								},
								{
									data : 'sReceivedPaymentsIdentifyType.title'
								},
								{
									data : 'sReceivedPaymentsIdentifyNumber'
								},
								{
									data : 'sReceivedPaymentsName'
								},
								{
									data : 'sReceivedPaymentsAccount'
								},
								{
									data : 'sReceivedPaymentsCustomerName'
								},
								{
									data : 'sReceivedPaymentsCustomerAccount'
								},
								{
									data : 'dReceivedPaymentsDate',
									render: function (data, type, full, meta) {
				                        if (data)
				                            return moment(data).format('YYYY/MM/DD');
				                        else
				                            return '';
				                    }
								},
								{
									data : 'sReceivedPaymentTradeType'
								},
								{
									data : 'nReceivedPaymentActualPrice'
								},
								{
									data : 'nBeLeftOverPrice'
								},
								{
									data : 'sReceivedPaymentPersonIdName'
								},
								{
									data : 'sReceivedPaymentMemo',
									 render:function(data,type,full,meta){
					   					 return '<font title="'+data+'">'+stringCut(data, 10)+'</font>';
					   	             }
								},
								{
									data : 'sReceivedPaymentTradeWay'
								},
								{
									data : "",
									defaultContent : "",
									render : function(data, type, full, meta) {
										var operate = "";

										operate = '<button type="button" class="btn btn-sm btn-default editBtn" >编辑</button>'
												+ '<button type="button" class="btn btn-sm btn-default delBtn">删除</button>';

										return operate;
									}
								} ],
						"drawCallback" : function(oSettings) {
							var k = 0;
							var total = oSettings.aiDisplay.length
									+ oSettings._iDisplayStart;
							for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
								$(
										'td:eq(0)',
										oSettings.aoData[oSettings.aiDisplay[k]].nTr)
										.text(i + 1);
								k++;
							}

							receivedPaymentsTotalInfoTable.off('click', 'tr');
							receivedPaymentsTotalInfoTable
									.on(
											'click',
											'tr',
											function(event) {
												event = event || window.event;
												var target = event.target
														|| event.scrElement;
												var data = receivedPaymentsTotalInfoTable.row(this)
														.data();
												if (target.className
														.indexOf('fa-remove') != -1
														|| target.className
																.indexOf('delBtn') != -1) {
													deleteReceivedPaymentTotalInfo(data);
												}

												if (target.className
														.indexOf('fa-pencil') != -1
														|| target.className
																.indexOf('editBtn') != -1) {
													editReceivedPaymentTotalInfo(data);
												}

												return;
											});
						}
					})
					});					
	jQuery("#addBtn").click(function() {
		jQuery("#id").val("");
		jQuery("#sReceivedPaymentsNetworkNumer").val("");
		jQuery('#sReceivedPaymentsIdentifyType').val("");
		jQuery("#sReceivedPaymentsIdentifyNumber").val("");
		jQuery("#sReceivedPaymentsName").val("");
		jQuery("#sReceivedPaymentsAccount").val("");
		jQuery('#sReceivedPaymentsCustomerName').val("");
		jQuery('#sReceivedPaymentsCustomerAccount').val("");
		jQuery('#dReceivedPaymentsDate').val("");
		jQuery('#sReceivedPaymentTradeType').val("");
		jQuery('#nReceivedPaymentActualPrice').val("");
		jQuery('#sReceivedPaymentMemo').val("");
		jQuery('#sReceivedPaymentTradeWay').val("");
		jQuery('#sReceivedPaymentPersonId').val("");
		jQuery("#sReceivedPaymentPersonId").select2().trigger("change");
		jQuery("#ReceivedPaymentsTotalInfoModal").modal('show');
	})

	var deleteReceivedPaymentTotalInfo = function(data) {
		if (!data)
			return;
		$.springLemonDialog({
			content : '确认删除' + data.sReceivedPaymentsIdentifyNumber + '？',// 确认文本
			clickButton : function(sender, modal, index) {// 按钮事件处理
				$(this).closeDialog(modal);
				if(index==0)
				{
					var url =window.rootPath
					+ '/ui/ReceivedPaymentTotalInfo/'+data.id;
					jQuery.ajax({
		               	type : "post",
		               	url: encodeURI(url),            	
		               	dataType : 'json',          
		               	contentType: 'application/json;charset=utf-8',
		               	success : function(d) {
		               		if (d.code == 1) {
								message("删除失败")
							} else {
								receivedPaymentsTotalInfoTable.draw(false);
							}
		               	}
		        	});
				}
				else{					
				}
			}
		});
	};

	 var editReceivedPaymentTotalInfo = function(data) {
		if (!data)
			return;
		jQuery("#id").val(data.id);
		jQuery("#sReceivedPaymentsNetworkNumer").val(data.sReceivedPaymentsNetworkNumer);		
		jQuery('#sReceivedPaymentsIdentifyNumber').val(data.sReceivedPaymentsIdentifyNumber);
		jQuery('#sReceivedPaymentsName').val(data.sReceivedPaymentsName);
		jQuery('#sReceivedPaymentsAccount').val(data.sReceivedPaymentsAccount);
		jQuery('#sReceivedPaymentsCustomerName').val(data.sReceivedPaymentsCustomerName);
		jQuery('#sReceivedPaymentsCustomerAccount').val(data.sReceivedPaymentsCustomerAccount);
		if(data.dReceivedPaymentsDate){
			jQuery('#dReceivedPaymentsDate').val(moment(data.dReceivedPaymentsDate).format('YYYY-MM-DD'));
			}		
		jQuery('#sReceivedPaymentTradeType').val(data.sReceivedPaymentTradeType);
		jQuery('#nReceivedPaymentActualPrice').val(data.nReceivedPaymentActualPrice);
		jQuery('#sReceivedPaymentMemo').val(data.sReceivedPaymentMemo);
		jQuery('#sReceivedPaymentTradeWay').val(data.sReceivedPaymentTradeWay);
		
		var tempsReceivedPaymentsIdentifyType=data.sReceivedPaymentsIdentifyType;
		
		if(tempsReceivedPaymentsIdentifyType) 
		{	
			jQuery('#sReceivedPaymentsIdentifyType').val(tempsReceivedPaymentsIdentifyType.id);
		}
		jQuery('#sReceivedPaymentPersonId').val(data.sReceivedPaymentPersonId);
		jQuery("#sReceivedPaymentPersonId").select2().trigger("change");
		
		jQuery('#ReceivedPaymentsTotalInfoModal').modal('show');
	};	
	
	
	 // 点击查询按钮
	 jQuery('.searchBtn').on('click', function() {
	 serverData.key = jQuery("#keyword").val();
	 receivedPaymentsTotalInfoTable.draw();
	 });
	 // 撤销查询
	 jQuery('.resetBtn').on('click', function() {
	 jQuery("#keyword").val("");
	 serverData.key = jQuery("#keyword").val();
	 receivedPaymentsTotalInfoTable.draw();
	 });
	// // 保存按钮
	jQuery('.saveBtn').on('click', function() {
		if (jQuery("#ReceivedPaymentsTotalInfoEditForm") && !jQuery("#ReceivedPaymentsTotalInfoEditForm").Validform({
			tiptype : 3,
			showAllError : true
		}).check(false)) {
			return;
		}

		var id = jQuery("#id").val();
		var sReceivedPaymentsNetworkNumer = jQuery("#sReceivedPaymentsNetworkNumer").val();
		var sReceivedPaymentsIdentifyType = jQuery('#sReceivedPaymentsIdentifyType').val();
		var sReceivedPaymentsIdentifyNumber = jQuery('#sReceivedPaymentsIdentifyNumber').val();
		var sReceivedPaymentsName = jQuery('#sReceivedPaymentsName').val();
		var sReceivedPaymentsAccount = jQuery('#sReceivedPaymentsAccount').val();
		var sReceivedPaymentsCustomerName = jQuery('#sReceivedPaymentsCustomerName').val();
		var sReceivedPaymentsCustomerAccount = jQuery('#sReceivedPaymentsCustomerAccount').val();
		var dReceivedPaymentsDate = jQuery('#dReceivedPaymentsDate').val();
		var sReceivedPaymentTradeType = jQuery('#sReceivedPaymentTradeType').val();
		var nReceivedPaymentActualPrice = jQuery('#nReceivedPaymentActualPrice').val();
		var sReceivedPaymentMemo = jQuery('#sReceivedPaymentMemo').val();
		var sReceivedPaymentTradeWay = jQuery('#sReceivedPaymentTradeWay').val();
		var sReceivedPaymentPersonId = jQuery('#sReceivedPaymentPersonId').val();
		
		// 判断id，新增还是编辑
		var d = {
			"sReceivedPaymentsNetworkNumer" : sReceivedPaymentsNetworkNumer,
			"sReceivedPaymentsIdentifyNumber" : sReceivedPaymentsIdentifyNumber,
			"sReceivedPaymentsName" : sReceivedPaymentsName,
			"sReceivedPaymentsAccount" : sReceivedPaymentsAccount,
			"sReceivedPaymentsCustomerName" : sReceivedPaymentsCustomerName,
			"sReceivedPaymentsCustomerAccount" : sReceivedPaymentsCustomerAccount,
			"dReceivedPaymentsDate" : dReceivedPaymentsDate,
			"sReceivedPaymentTradeType" : sReceivedPaymentTradeType,
			"nReceivedPaymentActualPrice" : nReceivedPaymentActualPrice,
			"sReceivedPaymentMemo" : sReceivedPaymentMemo,
			"sReceivedPaymentPersonId" : sReceivedPaymentPersonId,
			"sReceivedPaymentTradeWay" : sReceivedPaymentTradeWay			
		}
		if (sReceivedPaymentsIdentifyType)
			d.sReceivedPaymentsIdentifyType = {
				"id" : sReceivedPaymentsIdentifyType
			};
		
		var method = "";
		if (id == "") {
			method = 'newObject';
		} else {
			d.id = id;
			method = 'editObject';
		}
		var url =window.rootPath
		+ '/ui/ReceivedPaymentTotalInfo/'+method;
		jQuery.ajax({
               	type : "post",
               	url: encodeURI(url),
                data: JSON.stringify(d),             	
               	dataType : 'json',          
               	contentType: 'application/json;charset=utf-8',
               	success : function(data) {
               		if (data.code == 0) {
        				receivedPaymentsTotalInfoTable.draw();
        				jQuery('#ReceivedPaymentsTotalInfoModal').modal('hide');
        			} else {
        				message("凭证号已存在");
        			}
               	}
        	});
	});
	
	//EXCEL导入
    jQuery('.importReplacementList').on("click",function(){
    	var root = window.rootPath;
		WebFilesUploader({
			_webuploader : {
				server : root + '/ui/ReceivedPaymentTotalInfo/import', 
				swf : root
						+ '/resources/poweradminjs/webupload/webfilesuploader/Uploader.swf',
				chunked:false,
				multiple : false,
				accept:{
					title : "EXCEL导入",
					extensions : "xls,xlsx"
				}
			}
		}, function(jsons) {
			receivedPaymentsTotalInfoTable.draw();
		});
		
		WebFilesUploader.uploader.on("uploadSuccess",function(file,res){
	    	alert(res.count);
	    });
    });
    
    

})();